package com.zjdiepu.www.biz.base;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.base.SysUserRoleMapper;
import com.zjdiepu.www.ibiz.base.ISysUserRoleService;
import com.zjdiepu.www.model.base.dto.SysUserRole;
import com.zjdiepu.www.model.base.ext.SysUserRoleExt;

/**
 * 用户角色对应表Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年04月05日 16时02分<br/>
 * @since 1.0<br/>
 */
@Service
public class SysUserRoleServiceImpl extends BaseService implements ISysUserRoleService {
	public static final Logger logger = LoggerFactory.getLogger(SysUserRoleServiceImpl.class);

	@Autowired
	private SysUserRoleMapper sysUserRoleMapper;
	
	@Override
	public void saveUpdateAndDelete(SysUserRole sysUserRole, OperateType operType) {
		switch (operType) {
		case add:
			sysUserRole.initInsertData(sysUserRole.getModifier(), sysUserRole.getModifierName());
			sysUserRoleMapper.insert(sysUserRole);
			break;
		case edit:
			sysUserRole.initUpdateData(sysUserRole.getModifier(), sysUserRole.getModifierName());
			// 影响的记录数
			int updateCount = sysUserRoleMapper.updateByPrimaryKeySelective(sysUserRole);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			sysUserRole.initDeleteData(sysUserRole.getModifier(), sysUserRole.getModifierName());
			// 影响的记录数
			int deleteCount = sysUserRoleMapper.updateByPrimaryKeySelective(sysUserRole);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<SysUserRole> selectByExt(SysUserRoleExt sysUserRoleExt) {
		return sysUserRoleMapper.selectByExt(sysUserRoleExt);
	}
	
	@Override
	public int countByExt(SysUserRoleExt sysUserRoleExt){
		return sysUserRoleMapper.countByExt(sysUserRoleExt);
	}
	
	@Override
	public SysUserRole selectById(String id) {
		return sysUserRoleMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			sysUserRoleMapper.clearEmpty(fieldNames, id);
		}
	}

	@Override
	public void save(List<SysUserRole> sysUserRoles) {
		if(CollectionUtils.isNotEmpty(sysUserRoles)){
			sysUserRoleMapper.insertBatch(sysUserRoles);
		}
	}

	@Override
	public void delete(SysUserRole sysUserRole) {
		sysUserRoleMapper.delete(sysUserRole);
	}
}
